package cn.com.bluemoon.daps.system.controller;

import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.datascope.IgnorePermission;
import cn.com.bluemoon.daps.common.datascope.log.enums.ModuleObject;
import cn.com.bluemoon.daps.common.datascope.log.enums.OperateType;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.system.dto.OperateLogPageDTO;
import cn.com.bluemoon.daps.system.entity.DapOperateLog;
import cn.com.bluemoon.daps.system.service.DapOperateLogService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dapSystem")
@Api(tags = {"操作日志管理"})
@Slf4j
public class DapOperateLogController {

    @Autowired
    DapOperateLogService dapOperateLogService;

    @ApiOperation("操作日志查询")
    @PostMapping(value = "/operate/page")
    @BmPermission(value = "system:operate:log:page")
    public ResultBean<IPage<DapOperateLog>> operateLogPage(@RequestBody OperateLogPageDTO dto) {
        IPage<DapOperateLog> logIPage = dapOperateLogService.selectOperateLogPage(dto);
        return ResultBean.ok(logIPage);
    }

    @ApiOperation("操作日志详情")
    @GetMapping(value = "/operate/info")
    @BmPermission(value = "system:operate:info")
    public ResultBean<DapOperateLog> operateLogInfo(@RequestParam("id") Integer id){
        DapOperateLog operateLog = dapOperateLogService.getBaseMapper().selectById(id);
        return ResultBean.ok(operateLog);
    }

    @ApiOperation("新增操作日志")
    @PostMapping(value = "/operate/addOperateLog")
    @Async
    public ResultBean<Boolean> addOperateLog(@RequestBody DapOperateLog operateLog){
        return ResultBean.ok(dapOperateLogService.insertOperateLog(operateLog));
    }

    @ApiOperation("获取操作类型集合")
    @GetMapping("/getOperateTypeList")
    @IgnorePermission(name="获取操作类型集合")
    public ResultBean<List<String>> getOperateTypeList(){
        List<String> operateTypeList = new ArrayList<>();
        for (OperateType operateType : OperateType.values()) {
            operateTypeList.add(operateType.getType());
        }
        return ResultBean.ok(operateTypeList);
    }

    @ApiOperation("获取模块名称集合")
    @GetMapping("/getModuleNameList")
    @IgnorePermission(name="获取模块名称集合")
    public ResultBean<List<String>> getModuleNameList(){
        List<String> moduleNameList = new ArrayList<>();
        for (ModuleObject module : ModuleObject.values()) {
            moduleNameList.add(module.getModule());
        }
        return ResultBean.ok(moduleNameList);
    }
}
